package com.ecust.ml.cardriving.ivl;

import java.awt.image.VolatileImage;

import libsvm.svm;
import Jama.Matrix;

import com.fly.lib.util.IntUtil;

public class Policy {
	
	public static final int STATE_COUNT = 5000;
	
	public static int lastAction = 5;
	
	private Matrix valueMatrix = new Matrix(STATE_COUNT, 1);
	
	public Policy() {
		for(int i=0; i<valueMatrix.getRowDimension(); i++) {
			valueMatrix.set(i, 0, 5);
		}
	}

	public Policy(Matrix actionMatrix) {
		this.valueMatrix = actionMatrix;
	}
	
	public Policy(double[][] dataArray) {
		double[][] randomPolicyArray = new double[STATE_COUNT][1];
		for(int i=0; i<dataArray.length; i++) {
			int laneOn = (int) dataArray[i][0];
			int leftDistance = (int) dataArray[i][1];
			int middleDistance = (int) dataArray[i][2];
			int rightDistance = (int) dataArray[i][3];
			State state = new State(laneOn, leftDistance, middleDistance, rightDistance);
			int stateIndex = InverseReinfocementLearning.getStateIndexByState(state);
			randomPolicyArray[stateIndex-1000][0] = laneOn;
		}
		for(int i=0; i<randomPolicyArray.length; i++) {
			if(randomPolicyArray[i][0] == 0) {
				if(i == 0) {
					randomPolicyArray[i][0] = 3;
				} else {
					if(i+1 < randomPolicyArray.length) {
						int nextAction = (int) randomPolicyArray[i+1][0];
						int lastAction = (int) randomPolicyArray[i-1][0];
						if(nextAction != 0) {
							if(lastAction-1 > nextAction) {
								randomPolicyArray[i][0] = IntUtil.getRandomInt(nextAction, lastAction-1);
							} else {
								randomPolicyArray[i][0] = IntUtil.getRandomInt(lastAction-1>0?lastAction-1:1, nextAction);
							}
						} else {
							randomPolicyArray[i][0] = lastAction;
						}
					} else {
						randomPolicyArray[i][0] = randomAction((int) randomPolicyArray[i-1][0]);
					}
				}
				if(randomPolicyArray[i][0] == 0) {
					System.out.println("policy=0");
				}
			}
		}
		valueMatrix = new Matrix(randomPolicyArray);
	}
	
	private int randomAction(int laneOn) {
		int result = 0;
		if(laneOn == 1) {
			result = IntUtil.getRandomInt(1, 2);
		} else if(laneOn == 5) {
			result = IntUtil.getRandomInt(4, 5);
		} else {
			result = IntUtil.getRandomInt(laneOn-1, laneOn+1);
		}
		if(result ==0) {
			System.out.println();
		}
		
		return result;
	}
	
	private int randomAction() {
		/*
		int randormAction = -1;
		if(lastAction == -1) {
			randormAction = IntUtil.getRandomInt(1, 5);
		} else {
			if(lastAction == 1) {
				randormAction = IntUtil.getRandomInt(1, 2);
			} else if(lastAction == 5) {
				randormAction = IntUtil.getRandomInt(4, 5);
			} else {
				randormAction = IntUtil.getRandomInt(lastAction-1, lastAction+1);
			}
		}
		lastAction = randormAction;
		return randormAction;
		*/
		return IntUtil.getRandomInt(4,5);
	}

	public Matrix getValueMatrix() {
		return valueMatrix;
	}

	public void setValueMatrix(Matrix valueMatrix) {
		this.valueMatrix = valueMatrix;
	}

}
